﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using Common;
using AutoFacUtils;
using CashFlow.Db.Entity;
using CashFlow.Web.Filters;
using CashFlow.Web.Service;
using CashFlow.Web.Common;
using CashFlow.Web.Def;
using CashFlow.Web.Areas.ShopManage.Models.User;
using CashFlow.Web.Areas.ShopManage.Models.User.Result;
using CashFlow.Web.Areas.ShopManage.Validator.User;
using IUserService2 = CashFlow.Web.Areas.ShopManage.Service.IUserService;

namespace CashFlow.Web.Areas.ShopManage.Controllers
{
    [ShopManagerFilter]
    public class UserController : Controller
    {
        private IUserService GetUserService() { return AutoFacHelper.Get<IUserService>(); }
        private IUserService2 GetUserService2() { return AutoFacHelper.Get<IUserService2>(); }

        // GET: ShopManage/User
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult QueryShopAllUsers()
        {
            var userService = GetUserService();
            var ui = userService.GetLoginUser();
            var userService2 = GetUserService2();
            var ls = userService2.QueryShopAllUsers(ui.Id);
            return MyCommonTool.CreateOperationResult(true, "", ls);
        }

        [HttpPost]
        [ValidatorFilter(typeof(QueryUserModelValidator))]
        public ActionResult QueryByWhere(QueryUserModel model)
        {
            var service = GetUserService();
            var service2 = GetUserService2();
            var ui = service.GetLoginShopInfo();
            var ls = service2.QueryByWhere(
                        ui.ShopManagerUserId
                        , model.Phone
                        , model.Name
                        , model.InvitationCode
                        , model.Reference
                        , model.ReferenceInvitationCode
                        , model.State
                     );
            return MyCommonTool.CreateOperationResult(true, "", ls);
        }

        [HttpPost]
        [ValidatorFilter(typeof(ResetUserPwdModelValidator))]
        public ActionResult ResetPwd(ResetUserPwdModel model)
        {
            var service2 = GetUserService2();
            bool bSuccess = service2.ResetPwd(model.UserId, model.Pwd);
            return MyCommonTool.CreateOperationResult(bSuccess, "", null);
        }

        [HttpPost]
        [ValidatorFilter(typeof(UpdateUserStateModelValidator))]
        public ActionResult ResetState(UpdateUserStateModel model)
        {
            var service2 = GetUserService2();
            bool bSuccess = service2.ResetState(model.UserId, model.State);
            return MyCommonTool.CreateOperationResult(bSuccess, "", null);
        }

        [HttpPost]
        [ValidatorFilter(typeof(ResetShopManagerLevelPercentModelValidator))]

        public ActionResult ResetShopManagerLevelPercent(
            ResetShopManagerLevelPercentModel model)
        {
            var service2 = GetUserService2();
            bool bSuccess = service2.ResetShopManagerLevelPercent(
                                model.UserId
                                , model.Percent
                            );
            return MyCommonTool.CreateOperationResult(bSuccess, "", null);
        }

        [HttpPost]
        [ValidatorFilter(typeof(QueryUserStructureModelValidator))]
        public ActionResult QueryUserStructure(QueryUserStructureModel model)
        {
            var service = GetUserService();
            var ls = service.QueryShopUsersByUserId(model.UserId, null);
            var ls2 = ls.Select(x => Convert(x)).ToList();
            return MyCommonTool.CreateOperationResult(true, "", ls2);
        }

        [HttpPost]
        [ValidatorFilter(typeof(UpdateUserReferenceModelValidator))]
        public ActionResult UpdateUserReference(UpdateUserReferenceModel model)
        {
            var service2 = GetUserService2();
            var bSuccess = service2.UpdateUserReference(model.UserId, model.ReferenceInvitationCode);
            return MyCommonTool.CreateOperationResult(bSuccess, "", null);
        }

        [HttpPost]
        [ValidatorFilter(typeof(UpdateUserInfoModelValidator))]
        public ActionResult UpdateUserInfo(UpdateUserInfoModel model)
        {
            var service2 = GetUserService2();
            var result = service2.UpdateUserInfo(
                            model.UserId
                            , model.Name
                            , model.Phone
                         );
            return MyCommonTool.CreateOperationResult(
                        result == EUpdateUserInfoResult.Success,
                        EnumTool.GetDescription<EUpdateUserInfoResult, int>(result)
                        , null
                   );
        }

        private UserInfoModel3 Convert(UserInfoEntity entity)
        {
            return new UserInfoModel3
            {
                UserId = entity.Id,
                Name = entity.Name,
                InvitationCode = entity.InvitationCode,
                Phone = entity.Phone,
                ReferenceInvitationCode = entity.ReferenceInvitationCode
            };
        }
    }
}